package test
{

    // Copyright (c) 2002 Cunningham & Cunningham, Inc.
    // Released under the terms of the GNU General Public License version 2 or later.

import flexunit.framework.TestCase;
import flexunit.framework.TestSuite;

import com.fit4as.fit.RowFixture;
import com.fit4as.fit.TypeAdapter;
import com.fit4as.fit.Parse;

import com.fit4as.util.reflect.ClassUtil;
import com.fit4as.util.reflect.Method;


public class RowFixtureTest extends TestCase {

    public function RowFixtureTest(name : String) {
        super(name);
    }

    public static function suite():TestSuite {
        var ts:TestSuite = new TestSuite();
        ts.addTest(new RowFixtureTest("testMatch"));
        return ts;
    }

    public function testMatch() : void {

        /*
        Now back to the bug I found: The problem stems from the fact
        that java doesn't do deep equality for arrays. Little known to
        me (I forget easily ;-), java arrays are equal only if they
        are identical. Unfortunately the 2 sort methods returns a map
        that is directly keyed on the value of the column without
        considering this little fact. Conclusion there is a missing
        and a surplus row where there should be one right row.
        -- Jacques Morel
        */

        var fixture : TestRowFixture = new TestRowFixture();
        var method : Method = ClassUtil.getMethod( BusinessObject, "getStrings", [] );
        method.setComponentType( String );
        var arrayAdapter : TypeAdapter = TypeAdapter.onMethod( fixture, method );
        fixture.setColumnBindings([ arrayAdapter ]);

        var computed : Array = new Array();
        computed.push( new BusinessObject(["test"]));

        var expected : Array = new Array();
        expected.push( Parse.createParse("<tr><td>test</td></tr>", ['tr','td']));

        fixture.doMatch( expected, computed, 0);

        assertEquals("right", 1, fixture.counts.right);
        assertEquals("exceptions", 0, fixture.counts.exceptions);
        assertEquals("missing", 0, fixture.missing.length);
        assertEquals("surplus", 0, fixture.surplus.length);
    }
}
}

import com.fit4as.fit.RowFixture;


class TestRowFixture extends RowFixture {

    public function setColumnBindings(bindings : Array) : void
    {
        columnBindings = bindings;
    }

    public function doMatch(expected : Array, computed : Array, col : int) : void
    {
        match(expected, computed, col);
    }

    override public function query() : Array  // get rows to be compared
    {
        //trace("query");
        return [];
    }

    override public function getTargetClass() : Class             // get expected type of row
    {
        return BusinessObject;
    }
}

class BusinessObject 
{
	
    private var strs : Array;

    public function BusinessObject(strs : Array) {
        this.strs = strs;
    }

    public function getStrings() : Array {
        //trace("BusinessObject.getStrings " + strs);
        return strs;
    }
    
}

